{include file="common/head"}
<div class="max-w-lg mx-auto bg-white rounded shadow p-6 mt-8">
  <h2 class="text-xl font-bold mb-4 text-blue-600">计时报工</h2>
  {if isset($error) && $error}
    <div class="mb-4 p-3 bg-red-100 text-red-700 rounded text-center text-base font-bold">{$error}</div>
  {/if}
  {if isset($success) && $success}
    <div class="mb-4 p-3 bg-green-100 text-green-700 rounded text-center text-base font-bold">{$success}</div>
  {/if}
  <form method="post" class="space-y-4">
    <div>
      <label class="block mb-1 font-medium">选择分工任务</label>
      <select name="tallocationtime_id" class="w-full border rounded px-3 py-2" required>
        <option value="">请选择任务</option>
        {volist name="tasks" id="task"}
          <option value="{$task.id}">[{$task.work_date}] {$task.order_no|default=$task.order_id} {$task.product_name|default='-'} {$task.model_name|default='-'} {$task.process_name|default='-'}</option>
        {/volist}
      </select>
    </div>
    <div class="flex gap-4">
      <div class="flex-1">
        <label class="block mb-1 font-medium">工作日期</label>
        <input type="date" name="work_date" class="w-full border rounded px-3 py-2" required>
      </div>
      <div class="flex-1">
        <label class="block mb-1 font-medium">开始时间</label>
        <input type="time" name="start_time" class="w-full border rounded px-3 py-2">
      </div>
      <div class="flex-1">
        <label class="block mb-1 font-medium">结束时间</label>
        <input type="time" name="end_time" class="w-full border rounded px-3 py-2">
      </div>
    </div>
    <div>
      <label class="block mb-1 font-medium">工时</label>
      <input type="number" step="0.01" name="total_hours" class="w-full border rounded px-3 py-2" required>
      <p class="text-xs text-gray-500 mt-1">可自动根据时间计算</p>
    </div>
    <div>
      <label class="block mb-1 font-medium">备注</label>
      <input type="text" name="remark" class="w-full border rounded px-3 py-2" placeholder="可选" />
    </div>
    <button type="submit" class="w-full bg-blue-600 text-white py-2 rounded font-bold hover:bg-blue-700">提交报工</button>
  </form>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
  function calcHours() {
    var start = document.querySelector('input[name="start_time"]').value;
    var end = document.querySelector('input[name="end_time"]').value;
    var totalInput = document.querySelector('input[name="total_hours"]');
    if (start && end) {
      var s = start.split(':'), e = end.split(':');
      var startMins = parseInt(s[0]) * 60 + parseInt(s[1]);
      var endMins = parseInt(e[0]) * 60 + parseInt(e[1]);
      var diff = endMins - startMins;
      if (diff > 0) {
        var hours = (diff / 60).toFixed(2);
        totalInput.value = hours;
      }
    }
  }
  document.querySelector('input[name="start_time"]').addEventListener('change', calcHours);
  document.querySelector('input[name="end_time"]').addEventListener('change', calcHours);
});
</script>
{include file="common/footer"} 